#include "BBSprite.h"
#include "BBGraphics2D.h"


BBSprite::BBSprite( void )
	:m_pListKey(NULL),
	m_pObject(NULL)
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	m_pObject = CCSprite::create();
	addChild(m_pObject);
#endif
	
}

BBSprite::~BBSprite( void )
{
}

BBSprite* BBSprite::createSpriteWith( const char* plistKey,eReadFileType fileType )
{
	BBSprite* sprite = new BBSprite();
	if(sprite)
	{
		sprite->initObjectWith(plistKey,fileType);
		return sprite;
	}
	BB_SAFE_DELETE(sprite);
	return NULL;
}


BBSprite* BBSprite::createSpriteWith( const char* plistKey,const BBSize& size )
{
	BBSprite* sprite = new BBSprite();
	if(sprite)
	{
		sprite->initObjectWith(plistKey,size);
		return sprite;
	}
	BB_SAFE_DELETE(sprite);
	return NULL;
}

BBSprite* BBSprite::createSpriteWith( BBGraphics2D* texture )
{
	BBSprite* sprite = new BBSprite();
	if(sprite)
	{
		sprite->initObjectWith(texture);
		return sprite;
	}
	BB_SAFE_DELETE(sprite);
	return NULL;
}

BBSprite* BBSprite::createSpriteWith( BBGraphics2D* texture,const BBRectange& rect )
{
	BBSprite* sprite = new BBSprite();
	if(sprite)
	{
		sprite->initObjectWith(texture,rect);
		return sprite;
	}
	BB_SAFE_DELETE(sprite);
	return NULL;
}

BBSprite* BBSprite::createSpriteWith( BBGraphics2D* texture,const BBRectange& rect,const BBSize& size )
{
	BBSprite* sprite = new BBSprite();
	if(sprite)
	{
		sprite->initObjectWith(texture,rect,size);
		return sprite;
	}
	BB_SAFE_DELETE(sprite);
	return NULL;
}

void BBSprite::initObjectWith( const char* plistKey,eReadFileType fileType )
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	if(fileType == bNormalFile)
	{
		if(m_pObject)
			m_pObject->initWithFile(plistKey);
		else
			m_pObject = CCSprite::create(plistKey);
		CCRect rect = m_pObject->getTextureRect();
		m_RealRect.setRect(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
	}
	else if(fileType == bPlistFile)
	{
		if(m_pObject)
			m_pObject->initWithSpriteFrameName(plistKey);
		else
			m_pObject = CCSprite::createWithSpriteFrameName(plistKey);
		CCRect rect = m_pObject->getTextureRect();
		m_RealRect.setRect(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
	}
#endif
}


void BBSprite::initObjectWith( const char* plistKey,const BBSize& size )
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	initObjectWith(plistKey,bPlistFile);
	if(size.width == 0 && size.height == 0)
		return;
	setSize(size.width,size.height);
#endif
}


void BBSprite::initObjectWith( BBGraphics2D* texture )
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	BBRectange rect = RectZero;
	CCSize size = texture->getContentSize();
	rect.size.setSize(size.width,size.height);
	initObjectWith(texture,rect);
#endif
}

void BBSprite::initObjectWith( BBGraphics2D* texture,const BBRectange& rect )
{
	if(texture == NULL )
		return;
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	if(m_pObject)
		m_pObject->initWithTexture(texture->getGraphics2D(),CCRect(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height));
	else
		m_pObject = CCSprite::createWithTexture(texture->getGraphics2D(),CCRect(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height));
	CCRect tmpRect = m_pObject->getTextureRect();
	m_RealRect.setRect(tmpRect.origin.x,tmpRect.origin.y,tmpRect.size.width,tmpRect.size.height);
	setWidth(rect.size.width);
	setHeight(rect.size.height);
#endif
}

void BBSprite::initObjectWith( BBGraphics2D* texture,const BBRectange& rect,const BBSize& size )
{
	initObjectWith(texture,rect);
	if(size.width == 0 && size.height == 0)		
		return;
	setSize(size.width,size.height);
}

void BBSprite::setEnabled(bool var)
{
	m_bEnabel = var;
}

bool BBSprite::isEnabled()
{
	return m_bEnabel;
}

void BBSprite::setSelected(bool var)
{
	m_bSelected = var;
}

bool BBSprite::isSelected()
{
	return m_bSelected;
}

void BBSprite::setType(int var)
{
	m_iType = var;
}

int BBSprite::getType()
{
	return m_iType;
}

void BBSprite::setWidth(float var)
{
	m_fWidth = var;
}

float BBSprite::getWidth()
{
	return m_fWidth;
}

void BBSprite::setHeight(float var)
{
	m_fHeigth = var;
}

float BBSprite::getHeight()
{
	return m_fHeigth;
}

void BBSprite::setSize( float width,float height )
{
	m_fWidth = width;
	m_fHeigth = height;
}

BBSize BBSprite::getSize()
{
	return BBSize(m_fWidth,m_fHeigth);
}

void BBSprite::setLocation( float posX,float posY )
{
	setLocationX(posX);
	setLocationY(posY);
}

void BBSprite::setLocationX( float posX )
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	if(m_pObject)
		m_pObject->setPositionX(posX);
#endif
}

void BBSprite::setLocationY( float posY )
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	if(m_pObject)
		m_pObject->setPositionY(posY);
#endif
}

float BBSprite::getLocationX()
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	if(m_pObject)
		return m_pObject->getPositionX();
#endif
	return 0.0f;
}

float BBSprite::getLocationY()
{
#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
	if(m_pObject)
		m_pObject->getPositionY();
#endif
	return 0.0f;
}

